﻿using System;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.Storage.Streams;

namespace Microsoft.Media.CaptureClient.Sample
{
    internal class Logger
    {
        StorageFile storageFile;
        IRandomAccessStream ras;
        StreamWriter sw;

        public async Task InitializeAsync()
        {
            storageFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(Guid.NewGuid().ToString() + ".txt", CreationCollisionOption.ReplaceExisting);
            Debug.WriteLine(storageFile.Path);
            ras = await storageFile.OpenAsync(FileAccessMode.ReadWrite);
            sw = new StreamWriter(ras.AsStreamForWrite());
        }

        public void Log(string message)
        {
            Debug.WriteLine(message);
            sw.Write(DateTime.Now.ToString());
            sw.Write("\t");
            sw.WriteLine(message);
            sw.Flush();
        }

        public void Close()
        {
            sw.Dispose();
            sw = null;
            ras.Dispose();
            ras = null;
            storageFile = null;
        }
    }
}
